﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ClientEndpoint
{
    public class Sender
    {
	    internal Port[] ports { get; private set; }
	    static Sender rInstance_;
        static public Sender getInstance()
        {
	        return rInstance_ ?? (rInstance_ = new Sender());
        }
	    Sender()
        {
        }
	    static BitArray intToBitArray(int value, int size)
        {
            var bits = new BitArray(size, false);
            for (var i = 0; i < bits.Length; i++)
            {
                bits[i] = (value % 2 == 1);
                value >>= 1;
            }
            return bits;
        }
        public void sendMessage(string message,
                                int port,
                                int vpi,
                                int vci)
        {
            var messages = splitter(message, ATMConst.Const.ATM_DATA_SIZE);
            foreach (var m in messages)
            {
                var cell = new ATMCell.AtmCell(
                                        (new UTF8Encoding()).GetBytes(m),
                                        intToBitArray(0, (int)ATMCell.AtmCell.Size.Hec),
										intToBitArray(vpi, (int)ATMCell.AtmCell.Size.Vpi),
										intToBitArray(vci, (int)ATMCell.AtmCell.Size.Vci),
										intToBitArray(0, (int)ATMCell.AtmCell.Size.Payload),
										intToBitArray(0, (int)ATMCell.AtmCell.Size.Priority));
				Console.WriteLine(m);
                CableCloudEndPoint.getInstance().sendCellToNode(cell, 
                                                                port);                                        
            }
        }
        public void passMessage(byte[] message,
                                        int port,
                                        int vpi,
                                        int vci)
        {
                var cell = new ATMCell.AtmCell(
                                        message,
                                        intToBitArray(0, (int)ATMCell.AtmCell.Size.Hec),
                                        intToBitArray(vpi, (int)ATMCell.AtmCell.Size.Vpi),
                                        intToBitArray(vci, (int)ATMCell.AtmCell.Size.Vci),
                                        intToBitArray(0, (int)ATMCell.AtmCell.Size.Payload),
                                        intToBitArray(0, (int)ATMCell.AtmCell.Size.Priority));
                Console.WriteLine("message passed to port "+port);
                CableCloudEndPoint.getInstance().sendCellToNode(cell,
                                                                port);
        }
        IEnumerable<string> splitter(string message, int bytes = 48)
        {
			var tableSize = (message.Length % bytes == 0 ? (int)(message.Length / bytes) : (int)(message.Length / bytes + 1));
            var strings = new string[tableSize];
	        for (var i = 0; i < tableSize - 1; i++)
	        {
		        strings[i] = message.Substring(bytes*i, bytes);
	        }
	        strings[tableSize - 1] = message.Substring(bytes * (strings.Length - 1));
            return strings;
        }
    }
}
